package np.com.abhishek.scrambler.utils;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileView;

class ImageFileView extends FileView {
	ImageIcon jpgIcon = new ImageIcon("images/jpgIcon.gif");

	ImageIcon gifIcon = new ImageIcon("images/gifIcon.gif");

	ImageIcon tiffIcon = new ImageIcon("images/tiffIcon.gif");

	public String getName(File f) {
		return null; // let the L&F FileView figure this out
	}

	public String getDescription(File f) {
		return null; // let the L&F FileView figure this out
	}

	public Boolean isTraversable(File f) {
		return null; // let the L&F FileView figure this out
	}

	public String getTypeDescription(File f) {
		String extension = Utils.getExtension(f);
		String type = null;

		if (extension != null) {
			if (extension.equals(Utils.jpeg) || extension.equals(Utils.jpg)) {
				type = "JPEG Image";
			} else if (extension.equals(Utils.gif)) {
				type = "GIF Image";
			} else if (extension.equals(Utils.tiff)
					|| extension.equals(Utils.tif)) {
				type = "TIFF Image";
			}
		}
		return type;
	}

	public Icon getIcon(File f) {
		String extension = Utils.getExtension(f);
		Icon icon = null;

		if (extension != null) {
			if (extension.equals(Utils.jpeg) || extension.equals(Utils.jpg)) {
				icon = jpgIcon;
			} else if (extension.equals(Utils.gif)) {
				icon = gifIcon;
			} else if (extension.equals(Utils.tiff)
					|| extension.equals(Utils.tif)) {
				icon = tiffIcon;
			}
		}
		return icon;
	}
}

class JImageFilter extends FileFilter {

	// Accept all directories and all gif, jpg, or tiff files.
	public boolean accept(File f) {
		if (f.isDirectory()) {
			return true;
		}

		String extension = Utils.getExtension(f);
		if (extension != null) {
			if (extension.equals(Utils.tiff) || extension.equals(Utils.tif)
					|| extension.equals(Utils.gif)
					|| extension.equals(Utils.jpeg)
					|| extension.equals(Utils.jpg)) {
				return true;
			} else {
				return false;
			}
		}

		return false;
	}

	// The description of this filter
	public String getDescription() {
		return "Just Images";
	}
}

class ImagePreview extends JComponent implements PropertyChangeListener {
	private final static long serialVersionUID = 1L;

	ImageIcon thumbnail = null;

	File file = null;

	public ImagePreview(JFileChooser fc) {
		setPreferredSize(new Dimension(100, 50));
		fc.addPropertyChangeListener(this);
	}

	public void loadImage() {
		if (file == null) {
			return;
		}

		ImageIcon tmpIcon = new ImageIcon(file.getPath());
		if (tmpIcon.getIconWidth() > 90) {
			thumbnail = new ImageIcon(tmpIcon.getImage().getScaledInstance(90,
					-1, Image.SCALE_DEFAULT));
		} else {
			thumbnail = tmpIcon;
		}
	}

	public void propertyChange(PropertyChangeEvent e) {
		String prop = e.getPropertyName();
		if (prop.equals(JFileChooser.SELECTED_FILE_CHANGED_PROPERTY)) {
			file = (File) e.getNewValue();
			if (isShowing()) {
				loadImage();
				repaint();
			}
		}
	}

	public void paintComponent(Graphics g) {
		if (thumbnail == null) {
			loadImage();
		}
		if (thumbnail != null) {
			int x = getWidth() / 2 - thumbnail.getIconWidth() / 2;
			int y = getHeight() / 2 - thumbnail.getIconHeight() / 2;

			if (y < 0) {
				y = 0;
			}

			if (x < 5) {
				x = 5;
			}
			thumbnail.paintIcon(this, g, x, y);
		}
	}
}

public class ImageChooser {

	public static File file;

	public ImageChooser() {

		JFileChooser fc = new JFileChooser();
		fc.addChoosableFileFilter(new JImageFilter());
		fc.setFileView(new ImageFileView());
		fc.setAccessory(new ImagePreview(fc));

		int returnVal = fc.showDialog(null, "OK");

		if (returnVal == JFileChooser.APPROVE_OPTION) {
			file = fc.getSelectedFile();
		}
	}
}

class Utils {

	public final static String jpeg = "jpeg";

	public final static String jpg = "jpg";

	public final static String gif = "gif";

	public final static String tiff = "tiff";

	public final static String tif = "tif";

	/*
	 * Get the extension of a file.
	 */
	public static String getExtension(File f) {
		String ext = null;
		String s = f.getName();
		int i = s.lastIndexOf('.');

		if (i > 0 && i < s.length() - 1) {
			ext = s.substring(i + 1).toLowerCase();
		}
		return ext;
	}
}
